const cmHighIdPrefix = 'cmHighIdPref_';

var CultureMuncher_Inspector = {
	frameMaker: null,
	init: function() {
		CultureMuncher_Inspector.frameMaker = new CultureMuncher_FrameMaker(cmHighIdPrefix, 'Red');
	},
	onPageLoad: function(event) {
		
		var doc = event.originalTarget;
		if ((event.originalTarget.nodeName == '#document') &&
		    (event.originalTarget.defaultView.location.href == gBrowser.currentURI.spec)) {
			//alert('adding inspector');
			doc.addEventListener('mouseover', CultureMuncher_Inspector.paintactiveelement, true);
		}
		//addCss(content.document, ".highlight { border: 1px solid Red; -moz-box-align: center; -moz-box-shadow: 0px 0px 3px Red; }");
	},
	paintactiveelement: function(event) {
		// active element is either the last one
		if (event.target.id.substring(0, 13) == cmHighIdPrefix ||
		event.target instanceof HTMLHtmlElement ||
		!(event.target instanceof HTMLElement)) {
			return;
		}

		// hide frames
		CultureMuncher_Inspector.frameMaker.hideFrames();

		// find repetition element
		var elem = event.target;
		if (elem.parentNode != null && 
			(elem.parentNode.tagName.toLowerCase() == 'li' ||
			 elem.tagName.toLowerCase() == 'td')) {
			elem = elem.parentNode;
		}
		var path = get_XPath(elem);
		
		// check if this path is within the selected parent
		if (!CultureMuncher_Workflow.checkPath(path)) return;
		
		// remove indexer
		if (path.charAt(path.length - 1) == ']') {
			path = path.substring(0, path.lastIndexOf('['));
		}

		// set active lement in workflow
		CultureMuncher_Workflow.setActiveElement(elem);

		var elems = selectNodesXPath(content.document, path);

		for ( var i=0 ; i < elems.snapshotLength; i++ ) {
			// skip elements of the new borders
			var elet = elems.snapshotItem(i);
			if (elet.id.substring(0, 13) == cmHighIdPrefix) continue;
			
			// we need to create border for skipped elements in order not to create holes
			var border = CultureMuncher_Inspector.frameMaker.getFrame(i);			
			CultureMuncher_Inspector.frameMaker.adjustFrame(border, elet);
			
			if (elems.snapshotItem(i) == elem) {
				border.style.zIndex = -1;
				border.style.border = 'solid 1px Yellow';
			}
		}
	},
}

function CultureMuncher_FrameMaker(pfix, bcolor) {
	var prefix = pfix;
	var frames = [];
	var color = bcolor;

	this.getFrame = function(index, elem) {
		var id =  prefix + index;
		//dump('Getting frame for: ' + id + '\r\n');
		var border = content.document.getElementById(id);
		if (border == null) {
			//dump('Adding frame for: ' + id + '\r\n');
			// add new element to the page
			border = content.document.createElement("div");
			border.className = "highlight";
			border.id = "culturemuncher-select";
			border.style.position = "absolute";
			border.style.zIndex = -1;			
			border.id = id;

			var body = content.document.getElementsByTagName('body');

			if (body.length == 0)
				return;
			body[0].appendChild(border);
		}
		// position element
		if (elem != null) {
			this.adjustFrame(border, elem);
		}
		// set correct color
		border.style.border = '1px solid ' + color;
		// return result	
		return border;
	}
	this.hideFrames = function() {
		var i=0;
		var frame = content.document.getElementById(prefix + i);
		while (frame != null) {
			//dump(i + " ");
			
			frame.style.left = "-10px";
			frame.style.top = "-10px";
			frame.style.width = "1px";
			frame.style.height = "1px";

			//alert("Removing " + frame.id);
			
			frame = content.document.getElementById(prefix + ++i);
			
		}
	}
	this.adjustFrame = function(border, elem) {
		var pos = getPosition(elem);
		//dump("Positioning " + border.id + " at: " + pos.top + " " + pos.left + ", " + elem.offsetWidth + '\r\n');
		border.style.top = (pos.top) + "px"; // e.srcElement.offsetTop;
		border.style.left = (pos.left) + "px"; // e.srcElement.offsetLeft;
		border.style.width = (elem.offsetWidth - 2) + "px";
		border.style.height = (elem.offsetHeight - 2) + "px";
	}
}

// helper methods